package com.sdcs.api;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.sdcs.model.Discuss;
import com.sdcs.model.Course;
import com.sdcs.model.Homework;
import com.sdcs.model.Resource;
import com.sdcs.util.Constants;

public class HttpApiAccessor {

	/**
	 * 发送评论
	 * @param map
	 * @return
	 */
	public static String sendDiscuss(HashMap map) {
		String url =  Constants.WEB_APP_URL + "discuss.do?method=save" ;
		String ret = null;
		try { 
			ret = BaseAuthenicationHttpClient.doRequest(url, "", "" , map );
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ret;
	}

	/**
	 * 发送评论
	 * @param map
	 * @return
	 */
	public static String sendAttendance(HashMap map) {
		String url =  Constants.WEB_APP_URL + "attendance.do?method=save" ;
		String ret = null;
		try { 
			ret = BaseAuthenicationHttpClient.doRequest(url, "", "" , map );
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ret;
	}

	public static ArrayList getAllDiscussById(int id)  {
		String url =  Constants.WEB_APP_URL + "discuss.do?method=list&type=json&id=" + id;
		ArrayList ret = null;
		String jsonString;
		try {
			jsonString = BaseAuthenicationHttpClient.doRequest(url, "", "");
			JSONArray jsonArray = new JSONArray( jsonString  );
			ret = new ArrayList<Discuss>();
			if(jsonString.equals("[null]"))
				return ret ;
			for( int i = 0; i != jsonArray.length(); i++){
				JSONObject json = jsonArray.getJSONObject(i);
				Discuss object = jsonToDiscuss(json);
				ret.add(object);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ret;
	}

	private static Discuss jsonToDiscuss (JSONObject json) throws JSONException{
		Discuss discuss = new Discuss();
		discuss.setDiscuss_id(json.getInt("discuss_id"));
		discuss.setCourse_id(json.getInt("course_id"));
		discuss.setContent(json.getString("content"));
		discuss.setTbl_date(json.getString("tbl_date") );
		discuss.setAnswer( json.getString("answer") );
		return discuss;
	}
	 
	public static ArrayList<Homework> getZuoyeList(long lastId, int pageNo) throws Exception{
		String url = Constants.WEB_APP_URL + "homework_list.do?type=json";
		url = appendParams(url, lastId, pageNo);

		String jsonString = BaseAuthenicationHttpClient.doRequest(url, "", "");

		JSONArray jsonArray = new JSONArray(jsonString);		
		ArrayList<Homework> ret = new ArrayList<Homework>();
		for( int i = 0; i != jsonArray.length(); i++){
			JSONObject json = jsonArray.getJSONObject(i);
			Homework info = jsonToHomework(json);
			ret.add(info);
		}
		return ret;
	}

	public static ArrayList<Resource> getZiyuanList(long lastId, int pageNo) throws Exception{
		String url = Constants.WEB_APP_URL + "resource_list.do?type=json";
		url = appendParams(url, lastId, pageNo);
		
		System.out.println( " getUpdatesList url ::::::::::" + url );
		String jsonString = BaseAuthenicationHttpClient.doRequest(url, "", "");

		JSONArray jsonArray = new JSONArray(jsonString);		
		ArrayList<Resource> ret = new ArrayList<Resource>();
		for( int i = 0; i != jsonArray.length(); i++){
			JSONObject json = jsonArray.getJSONObject(i);
			Resource info = jsonToResouceTb(json);
			ret.add(info);
		}
		return ret;
	}
		
 
	public static ArrayList<Course> getFollowed(long lastId, int pageNo) throws Exception{
		String url = Constants.WEB_APP_URL + "course_list.do?type=json";
		return getUpdatesList(url,lastId,pageNo);
	}
	
//	
	private static ArrayList<Course> getUpdatesList(String url, long lastId, int pageNo) throws Exception{
		url = appendParams(url, lastId, pageNo);

		String jsonString = BaseAuthenicationHttpClient.doRequest(url, "", "");

		JSONArray jsonArray = new JSONArray(jsonString);		
		ArrayList<Course> ret = new ArrayList<Course>();
		for( int i = 0; i != jsonArray.length(); i++){
			JSONObject json = jsonArray.getJSONObject(i);
			Course food = jsonToInfo(json);
			ret.add(food);
		}
		return ret;
	}

	private static Course jsonToInfo(JSONObject json) throws JSONException{
		Course course = new Course();
		course.setCourse_description(json.getString("course_description"));
		course.setCourse_id(json.getInt("course_id"));
		course.setCourse_name(json.getString("course_name"));
		return course;
	}

	private static Resource jsonToResouceTb(JSONObject json) throws JSONException {
		Resource resource = new Resource();
		resource.setResource_id(json.getString("resource_id"));
		resource.setResource_name(json.getString("resource_name"));
		resource.setResource_describe(json.getString("resource_describe"));
		resource.setResource_path(json.getString("resource_path"));
		resource.setResource_date(json.getString("resource_date"));
		return resource;
	}
	private static Homework jsonToHomework(JSONObject json) throws JSONException {
		Homework homework = new Homework();
        homework.setHomework_id(json.getString("homework_id"));
        homework.setHomework_name(json.getString("homework_name"));
        homework.setHomework_describe(json.getString("homework_describe"));
        homework.setHomework_date(json.getString("homework_date"));
        homework.setHomework_path(json.getString("homework_path"));
        return homework;
	}


	private static String appendParams(String url, long lastId, int pageNo) {
		if(lastId != -1){
			url = "?last_id=" + lastId;
		}
		if(pageNo != -1){
			url = "&pageNo=" + pageNo;
		}
		return url;
	}

	
	
}



